package kr.hkit.powerjava.chapter25;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class ParseURLExample {

	public static void main(String[] args) {
		try {
			StringBuilder sb = new StringBuilder();
			System.out.println("url분석");
			URL myURL = new URL("http://www.hkit.kr/SUB/?ctId=49&eduCode=00000027:80");
			sb.append("protocol = " + myURL.getProtocol() + "\n");
			sb.append("authority = " + myURL.getAuthority() + "\n");
			sb.append("host = " + myURL.getHost() + "\n");
			sb.append("port = " + myURL.getPort() + "\n");
			sb.append("path = " + myURL.getPath() + "\n");
			sb.append("query = " + myURL.getQuery() + "\n");
			sb.append("filename = " + myURL.getFile() + "\n");
			sb.append("ref = " + myURL.getRef());
			System.out.println(sb);
			
			System.out.println("----------------------------------------------");
			System.out.println("문서 안의 내용을 보여줌");
			BufferedReader in = new BufferedReader(new InputStreamReader(myURL.openStream()));
			String line;
			
			while((line = in.readLine()) != null){
				System.out.println(line);
			}
			
			in.close();
		} catch (MalformedURLException e) {
			System.out.println("잘못된 주소입니다.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
